package com.crowdsourcing.controller;

import com.crowdsourcing.model.Msg;
import com.crowdsourcing.model.Task;
import com.crowdsourcing.model.User;
import com.crowdsourcing.service.ITaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author renbaojia 1406423298@qq.ocom
 * @version 1.0
 * @CREATED 2019/3/5 13:35:18
 * @des
 * @修改记录:
 **/
@Controller
@RequestMapping("/problem")
public class ProblemController {
    @Autowired
    HttpSession httpSession;
    @Autowired
    ITaskService service;


    /**
     * 查询发布的所有任务
     *
     * @return
     */

    @GetMapping("/all")
    public String publisher(HttpServletRequest request) {
        User user = (User) httpSession.getAttribute("user");
        List<Task> publisher = service.all(user.getId());
        request.setAttribute("info", publisher);
        request.setAttribute("status", "all");
        return "problem";
    }

    /**
     * 查询发布的，已处理的任务
     *
     * @return
     */
    @GetMapping("/receiveFinish")
    public String receiveFinish(HttpServletRequest request) {
        User user = (User) httpSession.getAttribute("user");
        List<Task> publisher = service.receiveFinish(user.getId());
        request.setAttribute("info", publisher);
        request.setAttribute("status", "receiveFinish");
        return "problem";
    }


    /**
     * 查询发布的未处理的任务
     *
     * @return
     */
    @GetMapping("/receiveNoFinish")
    public String receiveNoFinish(HttpServletRequest request) {
        User user = (User) httpSession.getAttribute("user");
        List<Task> publisher = service.receiveNoFinish(user.getId());
        request.setAttribute("info", publisher);
        request.setAttribute("status", "receiveNoFinish");
        return "problem";
    }

    /**
     * 保存任务
     *
     * @return
     */
    @ResponseBody
    @PostMapping("/save")
    public Msg save(@RequestParam("content") String content,@RequestParam("title") String title) {
        User user = (User) httpSession.getAttribute("user");
        service.save(content, user.getId(),title);
        return Msg.success();
    }
}
